package eclipse.lib.sockets;

import java.net.*;
import java.io.*;
/**
 * Class that represents a Client side Socket object.
 */
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
public final class ClientSocket
{
	private Socket			client;
	private int 			port = 4000;
	private String 			host = "127.0.0.1";
	private BufferedReader 	input;
	private BufferedWriter 	output;
	/** Creates a new Socket and connects using port 4000 to local host */
	public ClientSocket()
	{		
		connect(port, host);
	}
	/**
	 * Creates a new Socket using a specified port of the local host.
	 * @param iPort Port to connect to.
	 */
	public ClientSocket(int iPort)
	{
		setPort(iPort);
		connect(port, host);
	}
	/**
	 * Creates a new Socket using a specified IP of the host using port 4000
	 * @param sHost IP to connect to.
	 */
	public ClientSocket(String sHost)
	{
		setHost(sHost);
		connect(port, host);
	}
	/** 
	 * Creates a new Socket using a specified Port and IP.
	 * @param iPort Port of the Server
	 * @param sHost IP of the Server
	 */
	public ClientSocket(int iPort, String sHost)
	{
		setPort(iPort);
		setHost(sHost);
		connect(port, host);
	}	
	/**
	 * Creates a new Socket using a specified Port and IP.
	 * @param sHost Port of the Server
	 * @param iPort IP of the Server
	 */
	public ClientSocket(String sHost, int iPort)
	{
		setPort(iPort);
		setHost(sHost);
		connect(port, host);		
	}
	/**
	 * Creates a new Socket from an existing Socket.
	 */
	public ClientSocket(Socket sckClient)
	{
		try
		{
			client = sckClient;
			input 	= new BufferedReader
			(
				new InputStreamReader
				(
						client.getInputStream()
				)
			);
			output = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));	        
		}
		catch(Exception e)
		{
			eclipse.ErrorManager.handleError(e);
		}
	}
	/**
	 * Connects to the Server
	 * @param sHost IP of the Server
	 * @param iPort Port of the Server
	 */
	public final void connect(String sHost, int iPort)
	{
		connect(iPort, sHost);
	}
	/**
	 * Connects to the Server
	 * @param iPort Port of the Server
	 * @param sHost IP of the Server
	 */
	public final void connect(int iPort, String sHost)
	{		
		try
		{
			client 	= new Socket(host, port);
			input 	= new BufferedReader			
			(
				new InputStreamReader
				(
						client.getInputStream()
				)
			);
			output = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));				
		}
		catch(Exception e)
		{
			eclipse.ErrorManager.handleError(e);
		}
	}
	/** Forces reconnect to the Server */
	public final void reConnect()
	{
		close();
		client = null;
		connect(port, host);
	}	
	/** Returns a String net message recieved from the server. */
	public final String getString()
	{
		try
		{
			if(isConnected())					
				return input.readLine();			
			else
				return null;
		}
		catch(Exception e)
		{		
			close();
			client = null;
			return null;
		}
	}
	/** Returns an array of bytes recieved from the server. */
	public final byte[] getBytes()
	{				
		final byte[] comCode;
		final byte[] retFail = {0};
		try
		{
				comCode = new byte[16];
				client.getInputStream().read(comCode, 0, 16);
				return comCode;			
		}
		catch(Exception e)
		{
			eclipse.ErrorManager.handleError(e);			
			return retFail;
		}		
	}
	/**
	 * Sends a message to the Server
	 * @param bInput Array of bytes to send
	 */
	public final void sendMsg(byte[] bInput)
	{		
		try
		{
			client.getOutputStream().write(bInput, 0, bInput.length);
		}
		catch(Exception e)
		{
			eclipse.ErrorManager.handleError(e);
		}
	}
	/**
	 * Sends a message to the Server
	 * @param sInput String to send
	 */
	public final void sendMsg(String sInput)
	{
		try
		{
			output.write(sInput);
			output.flush();
		}
		catch(Exception e)
		{
			eclipse.ErrorManager.handleError(e);
		}
	}
	/** Returns if the Socket is connected to the Server */
	public final boolean isConnected()
	{
		try
		{			
			if(client.isConnected())					
				return true;
			else
				return false;
		}
		catch(Exception e)
		{
			close();
			return false;
		}
	}
	/** Closes all Socket connection */
	public final void close()
	{
		try 
		{
			client.close();
		}
		catch (Exception e)
		{	
			eclipse.ErrorManager.handleError(e);
		}
	}
	/** Returns the host IP. */
	public final String getHost()
	{
		return host;
	}
	/** Returns the Port of the */
	public final int getPort()
	{
		return port;
	}
	/** Sets the Host's IP. */
	public final void setHost(String sHost)
	{
		host = sHost;
	}
	/** Sets the Host's Port. */
	public final void setPort(int iPort)
	{
		port = iPort;
	}
	protected void finalize() throws Throwable
	{
		this.client.close();
		this.input.close();
		this.output.close();
	}
	
}
